//
//  HLUserDetailTagView.m
//  YueYue
//
//  Created by hulu001 on 2023/5/18.
//

#import "HLUserDetailTagView.h"
#import "HLUserDetailTagCell.h"

@interface HLUserDetailTagView ()
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) QMUIFloatLayoutView *labelsView;

@end

@implementation HLUserDetailTagView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColorWhite;
        [self setupView];
        
    }
    return self;
}

- (void)setupView {
    
    self.contentView = [[UIView alloc] init];
    _contentView.backgroundColor = HLBackgroundColor;
    _contentView.layer.masksToBounds = YES;
    _contentView.layer.cornerRadius = 8;
    [self addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(12, 12, 12, 12));
    }];
    
    
    self.iconView = [[UIImageView alloc] init];
    _iconView.image = UIImageMake(@"user_detail_tag");
    [self addSubview:_iconView];
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(28);
        make.top.mas_equalTo(0);
    }];
    
    self.signLabel = [[UILabel alloc] qmui_initWithFont:UIFontMediumMake(14) textColor:HLTextColor1];
    _signLabel.text = @"你自己觉得有，别人感觉不到你有，你就是没有";
    _signLabel.numberOfLines = 0;
    _signLabel.qmui_lineHeight = 22;
    [_contentView addSubview:_signLabel];
    [_signLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(26);
        make.right.mas_equalTo(-16);
    }];
    
    self.labelsView = [[QMUIFloatLayoutView alloc] init];
    _labelsView.padding = UIEdgeInsetsMake(6, 6, 6, 6);
    _labelsView.itemMargins = UIEdgeInsetsMake(6, 6, 6, 6);
    _labelsView.minimumItemSize = CGSizeMake(56, 26);// 以2个字的按钮作为最小宽度
    [self addSubview:_labelsView];
    [_labelsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_signLabel.mas_bottom).offset(16);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(88);
        make.bottom.mas_equalTo(-16);
    }];
    
}


- (void)setTags:(NSArray<NSString *> *)tags
{
    _tags = tags;
    [_labelsView qmui_removeAllSubviews];
    if (_tags.count > 0){
        for (NSString *tag in tags) {
            [_labelsView addSubview:[self createLabel:tag]];
        }
        NSInteger rows = ceil([tags count] / 5);
        if (rows == 0){
            rows = 1;
        }
        [_labelsView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(44 * rows);
        }];
    } else {
        [_labelsView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }
}

- (QMUIButton *)createLabel:(NSString *)label
{
    QMUIButton *labelButton = [[QMUIButton alloc] init];
    [labelButton setTitle:label forState:UIControlStateNormal];
    [labelButton setImage:UIImageMake(@"user_detail_label") forState:UIControlStateNormal];
    [labelButton setTitleColor:HLTextColor1 forState:UIControlStateNormal];
    [labelButton.titleLabel setFont:UIFontMake(12)];
    labelButton.layer.masksToBounds = YES;
    labelButton.layer.borderColor = HLLineColor.CGColor;
    labelButton.layer.borderWidth = 1;
    labelButton.layer.cornerRadius = 10;
    labelButton.imagePosition = QMUIButtonImagePositionLeft;
    labelButton.spacingBetweenImageAndTitle = 4;
    labelButton.contentEdgeInsets = UIEdgeInsetsMake(4, 8, 4, 8);
    return labelButton;
}

@end
